/*
 * File:                LoggingTools.cs
 * Authors:             
 * Company:             Sandia National Laboratories
 * Project:             MVC
 *
 * Copyright 2008, Sandia Corporation.  Under the terms of Contract
 * DE-AC04-94AL85000, there is a non-exclusive license for use of this work by
 * or on behalf of the U.S. Government. Export of this program may require a
 * license from the United States Government. See CopyrightHistory.txt for
 * complete details.
 *
 * Reviewers:
 * Review Date:
 * Review Comments:
 *
 * Revision History:
 *
 * $Log: LoggingTools.cs,v $
 * Revision 1.3  2008/10/23 21:28:50  bstitus
 * Reverted efforts earlier today to make a central exception logging method.  Instead, a constant string was created to reduce the proliferation in the code.  Bug #1262
 *
 * Revision 1.2  2008/10/23 14:19:53  bstitus
 * Added the file header since the file was being updated.  Added a new method to centralize exception handling code.  Bug #1262
 *
 *
 *
 */

using Microsoft.Practices.EnterpriseLibrary.Logging;

using System;
using System.Collections.Generic;
using System.Text;


namespace Sandia.MVCFramework.Common
{
    /// <summary>
    /// An enumeration of the different categories used when sending a message
    /// to the log.
    /// </summary>
    public enum LoggingCategories
    {
        /// <summary>
        /// Informational log entries.
        /// </summary>
        GENERAL,
        /// <summary>
        /// Program execution errors, also automatically generated by the
        /// exception handling mechanism.
        /// </summary>
        ERROR,
        /// <summary>
        /// For the purpose of debugging your code (NOTE: It is safe to leave
        /// debug log entries in production code, as we will simply filter out
        /// this category for release versions.
        /// </summary>
        DEBUG,
        /// <summary>
        /// Generated by MVC Framework when request routing is performed.
        /// </summary>
        REQUEST_ROUTING,
        /// <summary>
        /// Generated by MVC Framework when data publishing is performed.
        /// </summary>
        DATA_PUBLISH,
        /// <summary>
        /// Generated by MVC Framework when data accessors retrieve data.
        /// </summary>
        EXTERNAL_DATA_ACCESS
    }

    /// <summary>
    /// A set of tools used to assist with logging.
    /// </summary>
    /// <remarks>
    /// Taken from http://socrates.sandia.gov/twiki/bin/view/YuccaMtn/LoggingAndExceptionHandling
    /// on April 15, 2008:
    /// 
    /// We make use of the Microsoft Enterprise Library logging and exception 
    /// handling application blocks.
    /// 
    /// Categories:
    /// * General - informational log entries 
    /// * Error - program execution errors, also automatically generated by 
    ///   exception handling mechanism 
    /// * Debug - for the purpose of debugging your code (NOTE: It is safe to 
    ///   leave debug log entries in production code, as we will simply filter out
    ///   this category for released versions of the app) 
    /// * Request Routing - generated by MVC Framework when request routing is 
    ///   performed 
    /// * Data Publish - generated by MVC Framework when data publishing is 
    ///   performed 
    /// * External Data Access - generated by MVC Framework when data accessors 
    ///   retrieve data 
    /// 
    /// Priority levels:
    /// * 100 - Critical 
    /// * 50 - Informational 
    /// * 0 - Verbose info 
    /// 
    /// By and large you should be using level 50. Use level 0 for extra verbose
    /// info (typically for debugging). Level 100 is for when the app is going
    /// to die or something is going to be corrupted.
    /// </remarks>
    public class LoggingTools
    {
        /// <summary>
        /// A central definition for the policy for exception handling.
        /// </summary>
        public const string LoggingPolicy = "Default Policy";

        /// <summary>
        /// Sends a message with the given category and priority to the log.
        /// </summary>
        /// <param name="message">The message to send.</param>
        /// <param name="category">
        /// Categories:
        /// * General - informational log entries 
        /// * Error - program execution errors, also automatically generated by 
        ///   exception handling mechanism 
        /// * Debug - for the purpose of debugging your code (NOTE: It is safe to 
        ///   leave debug log entries in production code, as we will simply filter out
        ///   this category for released versions of the app) 
        /// * Request Routing - generated by MVC Framework when request routing is 
        ///   performed 
        /// * Data Publish - generated by MVC Framework when data publishing is 
        ///   performed 
        /// * External Data Access - generated by MVC Framework when data accessors 
        ///   retrieve data 
        /// </param>
        /// <param name="priority">
        /// Priority levels:
        /// * 100 - Critical 
        /// * 50 - Informational 
        /// * 0 - Verbose info 
        /// </param>
        public static void SendToLog(
            string message,
            LoggingCategories category,
            int priority)
        {
            LogEntry logEntry = new LogEntry();
            logEntry.Message = message;
            logEntry.Priority = priority;
            string categoryString = "";
            switch(category)
            {
                case LoggingCategories.DATA_PUBLISH:
                    categoryString = "Data Publish";
                    break;
                case LoggingCategories.DEBUG:
                    categoryString = "Debug";
                    break;
                case LoggingCategories.ERROR:
                    categoryString = "Error";
                    break;
                case LoggingCategories.EXTERNAL_DATA_ACCESS:
                    categoryString = "External Data Access";
                    break;
                case LoggingCategories.GENERAL:
                    categoryString = "General";
                    break;
                case LoggingCategories.REQUEST_ROUTING:
                    categoryString = "Request Routing";
                    break;
            }
            logEntry.Categories.Add(categoryString);
            Logger.Write(logEntry);
        }
    }
}
